//
//  DDOAuthWebViewController.m
//  DDOAuth
//
//  Created by zhn on 2017/3/10.
//  Copyright © 2017年 zhn. All rights reserved.
//

#import "DDOAuthWebViewController.h"
#import <WebKit/WebKit.h>
#define KOAuthBaseURL @"http://aicixi.dtdream.com/aicixi-web/oauth2/authorize"
@interface DDOAuthWebViewController()<UIWebViewDelegate,WKNavigationDelegate,UIWebViewDelegate>
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) WKWebView *wkWebView;
@property (nonatomic,assign) BOOL isOAuthing;
@property (nonatomic,strong) UIButton *closeButton;
@end


@implementation DDOAuthWebViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化返回按钮
    [self p_setupBackButton];
    // 初始化webview
    [self p_setupWebView];
}

- (void)dealloc {
    NSLog(@"webViewController 释放了");
}
#pragma mark - public methods



#pragma mark - target action
- (void)webViewGobackAction {
    if (self.useingWKwebView) {
        if ([self.wkWebView canGoBack]) {
            [self.wkWebView goBack];
            self.closeButton.hidden = NO;
        }else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else {
        if ([self.webView canGoBack]) {
            [self.webView goBack];
            self.closeButton.hidden = NO;
        }else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (void)popViewControllerAction {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - delegates
// ---- uiwebview delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSString *targetURL = request.URL.absoluteString;
    NSLog(@"%@",targetURL);
    if (self.webView) {
        if (self.isOAuthing) {return YES;}
        if ([targetURL hasPrefix:KOAuthBaseURL]) {
            NSMutableURLRequest *mutableRequest = [request mutableCopy];
            [mutableRequest addValue:@"ABDD84550C1555185C6C5DDFAA5B44EA" forHTTPHeaderField:@"client_data"];
            [self.webView loadRequest:mutableRequest];
            self.isOAuthing = YES;
            return NO;
        }
    }
    
    return YES;
}


// ---wkwebview delegate
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
    NSString *targetURL = webView.URL.absoluteString;
    NSLog(@"%@",targetURL);
    if (self.wkWebView) {
        if (self.isOAuthing) {return;}
        if ([targetURL hasPrefix:KOAuthBaseURL]) {
            NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:webView.URL];
            [mutableRequest addValue:@"ABDD84550C1555185C6C5DDFAA5B44EA" forHTTPHeaderField:@"client_data"];
            [self.wkWebView loadRequest:mutableRequest];
            self.isOAuthing = YES;
        }
    }
}

#pragma mark - pravite methods
- (void)p_setupBackButton {
    UIView *webViewMenuView = [[UIView alloc]init];
    webViewMenuView.frame = CGRectMake(0, 0, 200, 80);
    UIButton *popVCbutton = [[UIButton alloc]init];
    [popVCbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [popVCbutton setTitle:@"返回" forState:UIControlStateNormal];
    [popVCbutton addTarget:self action:@selector(webViewGobackAction) forControlEvents:UIControlEventTouchUpInside];
    [webViewMenuView addSubview:popVCbutton];
    popVCbutton.frame = CGRectMake(0, 0, 50, 80);
    
    UIButton *closeButton = [[UIButton alloc]init];
    closeButton.hidden = YES;
    [closeButton setTitle:@"关闭" forState:UIControlStateNormal];
    [closeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    closeButton.frame = CGRectMake(50, 0, 80, 80);
    [closeButton addTarget:self action:@selector(popViewControllerAction) forControlEvents:UIControlEventTouchUpInside];
    [webViewMenuView addSubview:closeButton];
    self.closeButton = closeButton;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:webViewMenuView];
}

- (void)p_setupWebView { 
    NSURL *url = [NSURL URLWithString:self.needOAuthURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    if (self.useingWKwebView) {
        [self.view addSubview:self.wkWebView];
        self.wkWebView.frame = self.view.bounds;
        [self.wkWebView loadRequest:request];
    }else {
        [self.view addSubview:self.webView];
        self.webView.frame = self.view.bounds;
        [self.webView loadRequest:request];
    }
}

#pragma mark - setters



#pragma mark - getters
- (UIWebView *)webView {
    if (_webView == nil) {
        _webView = [[UIWebView alloc]init];
        _webView.delegate = self;
    }
    return _webView;
}

- (WKWebView *)wkWebView {
    if (_wkWebView == nil) {
        _wkWebView = [[WKWebView alloc]init];
        _wkWebView.navigationDelegate = self;
    }
    return _wkWebView;
}


@end
